'use strict'

define(['jquery', 'bootstrap', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.initPage(data.handled);
        this.initHandle();
        this.initExport();
    },
    
    initPage: function(handled){
        $('select[name="siteId"]').select2({
            minimumResultsForSearch : -1,
            allowClear : false,
            width: '150'
        });

        $('select[name="siteId"]').change(function(){ 

            var siteId = $(this).children('option:selected').val();
            window.location.href = window.__CTX__.root + "/courseround-feedbacks?siteId=" + siteId + "&handled=" + (handled ? handled : "");

        })
    },
    
    initExport: function(){
        $(document).on('shown.bs.modal', ' #export', function(e){
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate

            $('button[type="submit"').click(function() {
                $(e.currentTarget).find("form").trigger("validate.ajax.submit");
                $.bt_validate.result[$.bt_validate.form_id];
            });
        })
    },
    
    initHandle: function(){
        $(document).on("shown.bs.modal", "#handle", function(e) {
            var $form = $(e.currentTarget).find("form");
          
            $form.bt_validate();
          
            $form.shiftform({
                beforeSubmit: function(el) {
                  $('form').trigger("validate.ajax.submit");
                       return $.bt_validate.result[$.bt_validate.form_id]
                }
            });
            
        })
    }
  }
})
